<?php
class CodeAction extends SecurityAction {
	function _initialize() {
		$this->assign ( "menuFirst", session ( 'menuFirst' ) );
		$this->assign ( "menuSecond", session ( 'menuSecond' ) );
	}
	
	// 我的赠送码
	function myclist() {
		$ob = D ( "Paycode" );
		$map = array ();
		$map ['uid'] = session ( 'info.uid' );
		
		$cpp = 30; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $map )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
		$list = $ob->where ( $map )->order ( 'code_id desc' )->limit ( $Page->firstRow . ',' . $Page->listRows )->getlist ();
		
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		$this->display ( "Code/myclist" );
	}
	
	// 赠送码列表页
	function clist() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		$db = M ( 'csl_user' );
		$draw ['status'] = 1;
		$supplier = $db->where ( $draw )->order ( 'uid asc' )->select ();
		
		$ob = D ("Paycode");
		
		//删除
		if($_GET['op']=='del' && $_GET['code_id']!='') {
		    $draw = array();
		    $draw['code_id'] = $_GET['code_id'];
		    $draw['order_no'] = '';
		    $ob->where($draw)->delete();
		    $this->redirect ( "/Admin/Code/clist/p/" . $_GET ['p'] );
		}
		
		
		$map = array ();
		if($_GET['uid']) {
		    $map['uid'] = $_GET['uid'];
		}
		$cpp = 30; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $map )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		$list = $ob->where ( $map )->order ( 'code_id desc' )->limit ( $Page->firstRow . ',' . $Page->listRows )->getlist ();
		
		$this->assign ( 'supplier', $supplier );
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		$this->display ( "Code/clist" );
	}
	
	// 编辑赠送码
	function edit() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		$ob = M ( 'csl_paycode' );
		$map ['code_id'] = $_GET ['code_id'];
		$info = $ob->where ( $map )->find ();
		
		$db = M ( 'csl_user' );
		$draw ['status'] = 1;
		$supplier = $db->where ( $draw )->order ( 'uid asc' )->select ();
		
		if ($_POST) {
			$data ['uid'] = $_POST ['uid'];
			$ob->where ( $map )->save ( $data );
			$this->redirect ( "/Admin/Code/clist/p/" . $_GET ['p'] );
		}
		
		$this->assign ( 'info', $info );
		$this->assign ( 'supplier', $supplier );
		$this->display ( "Code/edit" );
	}
	
	// 创建赠送码
	function add() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		$db = M ( 'csl_user' );
		$draw ['status'] = 1;
		$supplier = $db->where ( $draw )->order ( 'uid asc' )->select ();
		
		if ($_POST) {
			$ob = M ( 'csl_paycode' );
			$data ['uid'] = $_POST ['uid'];
			
			// 生成10张
			$i = 1;
			$j = 1;
			while ( $i <= 10 && $j <= 50 ) {
				$code = getRandChar ( 10 );
				$data ['code'] = $code;
				
				// 查找是否重复
				$map ['code'] = $code;
				$result = $ob->where ( $map )->find ();
				if (empty ( $result )) {
					$code_id = $ob->add ( $data );
					$i ++;
					$j ++;
				} else {
					$j ++;
				}
			}
			
			$this->redirect ( "/Admin/Code/clist/p/" . $_GET ['p'] );
		}
		
		$this->assign ( 'supplier', $supplier );
		$this->display ( "Code/add" );
	}
}

?>